package bikeAD;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;

import java.text.SimpleDateFormat;
import java.util.Date;

import static io.restassured.RestAssured.given;
import static io.restassured.config.EncoderConfig.encoderConfig;

public class common_utils {

    //登录方法，返回token
    public String login_return_token(String phoneNumber){
        setRedis redis = new setRedis();
        redis.setVcode(phoneNumber);//将手机验证码设置为1234
        Response response = given()
                .config(RestAssured.config().encoderConfig(encoderConfig().encodeContentTypeAs("application/graphql", ContentType.TEXT)))
                .header("Content-Type","application/graphql")
                .header("Cache-Control","no-cache")
                .body("mutation{\n  login(mobile:\""+phoneNumber+"\", code:\"1234\"){\n    basic{\n      city_ids,\n      id,\n      mobile,\n      nickname,\n      role_codes,\n      status\n    }\n    token{\n      refresh_token,\n      refresh_token_expire,\n      token,\n      token_expire,\n      user_id\n    }\n  }\n}")
                .when()
                .post("https://b2b-rompers-test.api.ofo.com/graphql");
        //断言登录接口的响应结果状态
        response.then().assertThat().statusCode(200);
        //利用接送Path获取token的值
        String token = response.jsonPath().getString("data.login.token.token");
        System.out.println("token = " + token);
        return token;
    }


    //新增订单,默认车辆数量为1，并返回订单id
    public String add_order_return_orderID(String phoneNum){
        String user_token = login_return_token(phoneNum);
        //新建一个日期类对象，获取当前的时间戳
        Date nowTime=new Date();
        SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String curTime=time.format(nowTime);
        //订单名称
        String order_name = "iphoneX" + curTime;
        System.out.println("order_name = " + order_name);
        //首次投放位置
        String launch_location = "shenzhen";

        //新增订单
        String graphql = "mutation{\n\tadd_sale_order(\n    launch_cycle:3,\n    launch_at:\"2018-12-01\",\n    launch_location:\""+launch_location+"\",\n    ad_form_id:5,\n    ad_amount:1,\n    city_id:23\n    name:\""+order_name+"\",\n    province_id:2\n  ){\n    ad_amount\n    ad_checked_amount\n    ad_form_id\n    ad_form_name\n    ad_id\n    ad_installed_amount\n    city_id\n    city_name\n    created_at\n    godown_at\n    launch_at\n    launch_cycle\n    launch_location\n    mobile\n    name\n    picture_urls\n    province_id\n    province_name\n    sale_order_id\n    sale_order_no\n    sale_order_status\n    updated_at\n    user_id\n    user_name\n  }\n}";
        //System.out.println("graphql = " + graphql);
        Response response = given()
                .config(RestAssured.config().encoderConfig(encoderConfig().encodeContentTypeAs("application/graphql", ContentType.TEXT)))
                .header("Content-Type","application/graphql")
                .header("Cache-Control","no-cache")
                .body(graphql)
                .when()
                .post("https://b2b-rompers-test.api.ofo.com/graphql?token="+user_token+"");
        //断言登录接口的响应结果状态
        response.then().assertThat().statusCode(200);
        //利用接送Path获取token的值
        String order_id = response.jsonPath().getString("data.add_sale_order.sale_order_id");
        System.out.println("order_id = " + order_id);
        return order_id;
    }


    //新增订单，并指定车辆数量，并返回订单id
    public String add_order_with_ad_mount_return_orderID(String phoneNum, int ad_mount){
        String user_token = login_return_token(phoneNum);
        //新建一个日期类对象，获取当前的时间戳
        Date nowTime=new Date();
        SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String curTime=time.format(nowTime);
        //订单名称
        String order_name = "iphoneX" + curTime;
        System.out.println("order_name = " + order_name);
        //首次投放位置
        String launch_location = "shenzhen";

        //新增订单
        String graphql = "mutation{\n\tadd_sale_order(\n    launch_cycle:3,\n    launch_at:\"2018-12-01\",\n    launch_location:\""+launch_location+"\",\n    ad_form_id:1,\n    ad_amount:"+ad_mount+",\n    city_id:23\n    name:\""+order_name+"\",\n    province_id:2\n  ){\n    ad_amount\n    ad_checked_amount\n    ad_form_id\n    ad_form_name\n    ad_id\n    ad_installed_amount\n    city_id\n    city_name\n    created_at\n    godown_at\n    launch_at\n    launch_cycle\n    launch_location\n    mobile\n    name\n    picture_urls\n    province_id\n    province_name\n    sale_order_id\n    sale_order_no\n    sale_order_status\n    updated_at\n    user_id\n    user_name\n  }\n}";
        //System.out.println("graphql = " + graphql);
        Response response = given()
                .config(RestAssured.config().encoderConfig(encoderConfig().encodeContentTypeAs("application/graphql", ContentType.TEXT)))
                .header("Content-Type","application/graphql")
                .header("Cache-Control","no-cache")
                .body(graphql)
                .when()
                .post("https://b2b-rompers-test.api.ofo.com/graphql?token="+user_token+"");
        //断言登录接口的响应结果状态
        response.then().assertThat().statusCode(200);
        //利用接送Path获取token的值
        String order_id = response.jsonPath().getString("data.add_sale_order.sale_order_id");
        System.out.println("order_id = " + order_id);
        return order_id;
    }


}
